package com.zlt.controller.seeker;

import com.zlt.controller.BaseController;
import com.zlt.dto.JobDTO;
import com.zlt.dto.JobTypeDTO;
import com.zlt.entity.JobType;
import com.zlt.service.JobService;
import com.zlt.service.impl.JobServiceImpl;
import com.zlt.util.ClassUtil;
import com.zlt.util.StringUtil;
import com.zlt.vo.JobVo;
import com.zlt.vo.Pager;
import com.zlt.vo.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet("/seeker/job/*")
public class JobController extends BaseController {
    private JobService jobService = new JobServiceImpl();
    @Override
    protected void defaultMethod(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { }
    
    /**
     * 按条件搜索岗位
     *
     * @param req
     * @param resp
     * @return
     * @throws ServletException
     * @throws IOException
     */
    protected Result search(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        JobDTO jobDTO = ClassUtil.resolveRequest(JobDTO.class,req);

        String pageNow = req.getParameter("pageNow");
        if (!StringUtil.isNotNull(pageNow)){
            pageNow = "1";
        }
        try {
            Pager<JobVo> pager = new Pager<>();
            pager.setPage(Integer.parseInt(pageNow));
            pager.setLimit(9l);

            pager = jobService.searchJob(pager,jobDTO);
            req.setAttribute("pager",pager);
            return Result.ok("搜索成功",pager);
        } catch (RuntimeException e){
            e.printStackTrace();
            return Result.error(e.getMessage());
        }

    }
    
    /**
     * 收藏意向岗位
     *
     * @param req
     * @param resp
     * @return
     * @throws ServletException
     * @throws IOException
     */
    protected Result collect(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String jid = req.getParameter("jid");
        String state = req.getParameter("state");
        try {
            boolean success = jobService.changeCollection(Integer.parseInt(jid),Integer.parseInt(state));
            if (success){
                return Result.ok("收藏成功");
            } else {
                return Result.error("收藏失败");
            }
        } catch (RuntimeException e){
            e.printStackTrace();
            return Result.notLogin("未登录");
        }
    }
    
    /**
     * 取消岗位收藏
     *
     * @param req
     * @param resp
     * @return
     * @throws ServletException
     * @throws IOException
     */
//    protected Result cancelCollect(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        String jid = req.getParameter("jid");
//        try {
//            boolean success = jobService.changeCollection(Integer.parseInt(jid),1);
//            if (success){
//                return Result.ok("取消收藏成功");
//            } else {
//                return Result.error("取消收藏失败");
//            }
//        } catch (RuntimeException e){
//            return Result.error("取消收藏失败");
//        }
//    }
    
    /**
     * 查看已收藏的所有岗位
     *
     * @param req
     * @param resp
     * @return
     * @throws ServletException
     * @throws IOException
     */
    protected Result checkCollect(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String page = req.getParameter("page");
        if (!StringUtil.isNotNull(page)){
            page = "1";
        }
        try {
            Pager<JobVo> pager = new Pager<>();
            Long limit = 6l;
            pager.setPage(Integer.parseInt(page));
            pager.setLimit(limit);
            pager = jobService.checkCollection(pager);
            return Result.ok("",pager);
        } catch (RuntimeException e){
            //TODO
            return Result.error("");
        }
    }
    
    /**
     * 单个岗位详情
     *
     * @param req
     * @param resp
     * @return
     * @throws ServletException
     * @throws IOException
     */
    protected Result detail(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String jid = req.getParameter("jid");
        try {
            Map<String,Object> job = jobService.checkOne(Integer.parseInt(jid));
            if (job != null && !job.isEmpty()){
                return Result.ok("查询成功",job);
            } else {
                return Result.error("查询失败");
            }
        } catch (RuntimeException e){
            return Result.error("查询失败");
        }
    }

    /**
     * 页面默认岗位（可按id倒序查询）
     * @param req
     * @param resp
     * @return
     * @throws ServletException
     * @throws IOException
     */
    protected Result list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String pageNow = req.getParameter("pageNow");
        if (!StringUtil.isNotNull(pageNow)){
            pageNow = "1";
        }
//        String pageSize = req.getParameter("pageSize");
//        if (!StringUtil.isNotNull(pageSize)){
//            pageSize = "5";
//        }
//        Pager<JobVo> pager = new Pager<>();
//        pager.setPage(Integer.parseInt(pageNow));
//        pager.setPageCount(Long.parseLong(pageSize));
//
//        req.setAttribute("pager",pager);
//        String page = req.getParameter("page");
//        if (!StringUtil.isNotNull(page)){
//            page = "1";
//        }
        try {
            Pager<JobVo> pager = new Pager<>();
            Long limit = 9l;
            pager.setPage(Integer.parseInt(pageNow));
            pager.setLimit(limit);
            pager = jobService.defaultJob(pager);
            req.setAttribute("pager",pager);
            return Result.ok("",pager);
        } catch (RuntimeException e){
            //TODO
            return Result.error("");
        }

    }
    /**
     * 获取所有岗位类型
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected Result jobType(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        JobTypeDTO jobTypeDTO = ClassUtil.resolveRequest(JobTypeDTO.class, req);
        if(jobTypeDTO!=null){ //查询数据
            List<JobType> jobTypes= null;
            try {
                jobTypes = jobService.selectAllTypes(Integer.parseInt(jobTypeDTO.getParentId()));
                if(!jobTypes.isEmpty()&&jobTypes!=null){
                    return Result.ok("查询成功",jobTypes);
                }else {
                    return Result.error("查询失败");
                }
            } catch (NumberFormatException e) {
                return Result.error("查询失败");
            }

        }else {return Result.error("查询失败");}
    }

    /**
     * 和HR沟通
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected Result addTalker(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String hrId = req.getParameter("hrId");
        String cid = req.getParameter("cid");
        String jid = req.getParameter("jid");
        try {
            boolean success = jobService.addTalk(hrId, cid, jid);
            return success ? Result.ok("成功") : Result.error("失败");
        } catch (RuntimeException e){
            return Result.error(e.getMessage());
        }

    }

    
}
